home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ARexxTools / fpl70.lha / src / libinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-19  |  7.9 KB  |  200 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  * fpl.library - A shared library interpreting script langauge.         *
  4.  * Copyright (C) 1992-1994 FrexxWare                                    *
  5.  * Author: Daniel Stenberg                                              *
  6.  *                                                                      *
  7.  * This program is free software; you may redistribute for non          *
  8.  * commercial purposes only. Commercial programs must have a written    *
  9.  * permission from the author to use FPL. FPL is *NOT* public domain!   *
  10.  * Any provided source code is only for reference and for assurance     *
  11.  * that users should be able to compile FPL on any operating system     *
  12.  * he/she wants to use it in!                                           *
  13.  *                                                                      *
  14.  * You may not change, resource, patch files or in any way reverse      *
  15.  * engineer anything in the FPL package.                                *
  16.  *                                                                      *
  17.  * This program is distributed in the hope that it will be useful,      *
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                 *
  20.  *                                                                      *
  21.  * Daniel Stenberg                                                      *
  22.  * Ankdammsgatan 36, 4tr                                                *
  23.  * S-171 43 Solna                                                       *
  24.  * Sweden                                                               *
  25.  *                                                                      *
  26.  * FidoNet 2:201/328    email:dast@sth.frontec.se                       *
  27.  *                                                                      *
  28.  ************************************************************************/
  29.  
  30. #define  _USEOLDEXEC_ 1
  31. #include <exec/types.h>
  32. #include <exec/nodes.h>
  33. #include <exec/memory.h>
  34. #include <exec/resident.h>
  35. #include <exec/libraries.h>
  36. #include <exec/execbase.h>
  37. #include <libraries/dos.h>
  38. #include <proto/exec.h>
  39. #include <proto/dos.h>
  40. #include <string.h>
  41.  
  42. #include "liballoc.h"
  43.  
  44. long _WBenchMsg;
  45.  
  46. /* Prototypes */
  47. ULONG __asm _LibExpunge( register __a6 struct MyLibrary *libbase );
  48. ULONG __asm _LibInit   ( register __a0 APTR seglist,
  49.                          register __d0 struct MyLibrary *libbase );
  50.  
  51. int  __saveds __asm __UserLibInit   (register __a6 struct MyLibrary *libbase);
  52. void __saveds __asm __UserLibCleanup(register __a6 struct MyLibrary *libbase);
  53.  
  54. int  __saveds __asm __UserDevInit   (register __d0 long unit,
  55.                                      register __a0 struct IORequest *ior,
  56.                                      register __a6 struct MyLibrary *libbase);
  57. void __saveds __asm __UserDevCleanup(register __a0 struct IORequest *ior,
  58.                                      register __a6 struct MyLibrary *libbase);
  59.  
  60. int  __saveds __asm __libfpinit     (register __a6 struct MyLibrary *libbase);
  61. void __saveds __asm __libfpterm     (register __a6 struct MyLibrary *libbase);
  62.  
  63. typedef LONG (*myPFL)();   /* pointer to function returning 32-bit int      */
  64.  
  65. /* library initialization table, used for AUTOINIT libraries                */
  66. struct InitTable {
  67.         ULONG        *it_DataSize;       /* library data space size         */
  68.         myPFL        *it_FuncTable;      /* table of entry points           */
  69.         APTR         it_DataInit;        /* table of data initializers      */
  70.         myPFL        it_InitFunc;        /* initialization function to run  */
  71. };
  72.  
  73. /* symbols generated by blink */
  74. extern char __far _LibID[];             /* ID string                        */
  75. extern char __far _LibName[];           /* Name string                      */
  76. extern char __far RESLEN;               /* size of init data                */
  77. extern long __far NEWDATAL;             /* size of global data              */
  78. extern long __far NUMJMPS;              /* number of jmp vectors to copy    */
  79. extern myPFL _LibFuncTab[];             /* my function table                */
  80. extern long __far _LibVersion;          /* Version of library               */
  81. extern long __far _LibRevision;         /* Revision of library              */
  82. #define MYVERSION ((long)&_LibVersion)
  83. #define MYREVISION ((long)&_LibRevision)
  84. #define DATAWORDS ((long)&NEWDATAL)     /* magic to get right tpye of reloc */ 
  85. #define SIZEJMPTAB ((long)libbase->ml_origbase->ml_numjmps)
  86.                                         /* size in bytes of jmp table       */
  87.  
  88. /* From libent.o, needed to determine where data is loaded by loadseg       */
  89. extern long far _Libmergeddata; 
  90.  
  91. #define MYLIBRARYSIZE ((sizeof(struct MyLibrary) +3) & ~3)
  92.  
  93. struct InitTable __far _LibInitTab =  {
  94.         (long *)(&RESLEN+MYLIBRARYSIZE),
  95.         _LibFuncTab,
  96.         NULL,                        /* will initialize my own data */
  97.         _LibInit,
  98. };
  99.  
  100. __asm ULONG _LibInit( register __a0 APTR seglist,
  101.                       register __d0 struct MyLibrary *libbase )
  102. {
  103.     long *reloc;
  104.     long *sdata;
  105.     char *ddata;
  106.     long nrelocs;
  107.  
  108.       
  109.     libbase->ml_SegList = (ULONG) seglist;
  110.  
  111.     /* init. library structure (since I don't do automatic data init.) */
  112.     libbase->ml_Lib.lib_Node.ln_Type = NT_LIBRARY;
  113.     libbase->ml_Lib.lib_Node.ln_Name =  _LibName;
  114.     libbase->ml_Lib.lib_Flags = LIBF_SUMUSED | LIBF_CHANGED;
  115.     libbase->ml_Lib.lib_Version = MYVERSION;
  116.     libbase->ml_Lib.lib_Revision = MYREVISION;
  117.     libbase->ml_Lib.lib_IdString = (APTR) _LibID;
  118.  
  119.      /* Start of copy of global data after structure */
  120.     ddata = (char *)libbase + MYLIBRARYSIZE; 
  121.  
  122.     sdata = (long *)&_Libmergeddata; /* where loadseg loaded the data */
  123.     memcpy(ddata, (void *)sdata, DATAWORDS*4);
  124.  
  125.     /* perform relocs if we want one global section for all programs */
  126.     /* that have this lib open. If we want a global section for each */
  127.     /* open, copy the relocs, and do them on each open call.         */
  128.     sdata = sdata + DATAWORDS;
  129.     nrelocs = *sdata;
  130.     sdata++;
  131.     while (nrelocs > 0)
  132.     {
  133.        reloc = (long *)((long)ddata + *sdata++);
  134.        *reloc += (long)ddata;
  135.        nrelocs--;
  136.     }
  137.     
  138.     if (__UserLibInit(libbase) != 0)
  139.        return NULL; /* abort if user init failed */
  140.  
  141.     return ( (ULONG) libbase );
  142. }
  143.  
  144. LONG __asm _LibOpen( register __a6 struct MyLibrary *libbase )
  145. {
  146. #ifndef ORIGINAL
  147.     libbase->ml_DosBase = OpenLibrary("dos.library", 33); /* require 33 */
  148.     if(! libbase->ml_DosBase )
  149.       return NULL; /* we failed! */
  150. #endif
  151.  
  152.     /* mark us as having another customer */
  153.     libbase->ml_Lib.lib_OpenCnt++;
  154.  
  155.     /* clear delayed expunges (standard procedure) */
  156.     libbase->ml_Lib.lib_Flags &= ~LIBF_DELEXP;
  157.  
  158.     return ( (LONG) libbase );
  159. }
  160.  
  161. ULONG __asm _LibClose( register __a6 struct MyLibrary *libbase )
  162. {
  163.     ULONG retval = 0;
  164.     
  165.     if (( --libbase->ml_Lib.lib_OpenCnt == 0 ) &&
  166.                         ( libbase->ml_Lib.lib_Flags & LIBF_DELEXP ))
  167.     {
  168.         /* no more people have me open,
  169.          * and I have a delayed expunge pending
  170.          */
  171.          retval = _LibExpunge( libbase ); /* return segment list        */
  172.     }
  173.  
  174.     CloseLibrary( libbase->ml_DosBase ); /* close dos.library */
  175.  
  176.     return (retval);
  177. }
  178.  
  179. ULONG __asm _LibExpunge( register __a6 struct MyLibrary *libbase )
  180. {
  181.     ULONG seglist = 0;
  182.     LONG  libsize;
  183.  
  184.     libbase->ml_Lib.lib_Flags |= LIBF_DELEXP;
  185.     if ( libbase->ml_Lib.lib_OpenCnt == 0 )
  186.     {
  187.         /* really expunge: remove libbase and freemem        */
  188.         __UserLibCleanup(libbase);
  189.         seglist = libbase->ml_SegList;
  190.  
  191.         Remove( (struct Node *) libbase);
  192.  
  193.         libsize = libbase->ml_Lib.lib_NegSize + libbase->ml_Lib.lib_PosSize;
  194.         FreeMem( (char *) libbase - libbase->ml_Lib.lib_NegSize,(LONG) libsize );
  195.     }
  196.  
  197.     /* return NULL or real seglist                                */
  198.     return ( (ULONG) seglist );
  199. }
  200.